package com.neuedu.mblog.service;

import com.neuedu.mblog.dao.CommentDao;
import com.neuedu.mblog.dao.PostDao;
import com.neuedu.mblog.dao.UserDao;
import com.neuedu.mblog.entiry.Comment;
import com.neuedu.mblog.entiry.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 项目：mblog_java4
 * 创建时间：  2021-12-29   9:21
 * 作者 :jshand
 * 描述 :
 */
public class UserService {
    private UserDao userDao = new UserDao();
    private CommentDao commentDao = new CommentDao();
    private PostDao postDao = new PostDao();

    /**
     * 查询集合不分页
     *
     * @param request
     * @return
     */
    public List<User> selectList(HttpServletRequest request) throws SQLException {
        //
        String sql = " SELECT  id,username,name,avatar,email,password,status,created,updated,last_login,gender,role_id,comments,posts,signature from mto_user   ";
        return userDao.selectList(sql, null);
    }


    public boolean regist(HttpServletRequest request) throws SQLException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        User user = new User();
        user.setUsername(username);
        //加密的密码
        user.setPassword(new BCryptPasswordEncoder().encode(password));
        user.setStatus(0);
        user.setCreated(new Date());
        user.setUpdated(user.getCreated());
        user.setRoleId(2);//所有前台注册的用户，默认角色为普通用户
        user.setComments(0);
        user.setPosts(0);

        return userDao.save(user) > 0;

    }



    public boolean login(HttpServletRequest request) throws SQLException {
        boolean success = false;
        String username = request.getParameter("username");
        String password = request.getParameter("password");


        String sql = "SELECT  id,username,name,avatar,email,password,status,created,updated,last_login,gender,role_id,comments,posts,signature from mto_user  " +
                "where username = ?";
        List<User> userList = userDao.selectList(sql,new Object[]{username});
        if(userList != null && userList.size()>0){
            User user = userList.get(0);

            //比较密码
            boolean match = new BCryptPasswordEncoder().matches(password , user.getPassword() );
            if(match){
                //登录成功
                success = true;

                //session中记录登录状态
                HttpSession session = request.getSession();
                session.setAttribute("user",user);

                //更新最后一次登录时间
                user.setLastLogin(new Date());
                userDao.update(user);
            }
        }

        return success;

    }


    public void postAndCommentNums(HttpServletRequest request) throws SQLException {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");

        Object params[] = new Object[]{user.getId()};

        String sql1 = "SELECT COUNT(1) FROM mto_post  t WHERE author_id = ? ";
        String sql2 = "SELECT COUNT(1) FROM mto_comment  t WHERE author_id = ? ";


        Long postCount = postDao.count(sql1,params);
        Long commentCount = commentDao.count(sql2,params);

        request.setAttribute("postCount",postCount);
        request.setAttribute("commentCount",commentCount);


    }



    public void postAndCommentNumsWithPost(HttpServletRequest request) throws SQLException {

        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");

        Object params[] = new Object[]{user.getId()};

        String sql1 = "SELECT COUNT(1) FROM mto_post  t WHERE author_id = ? ";
        String sql2 = "SELECT COUNT(1) FROM mto_comment  t WHERE author_id = ? ";


        Long postCount = postDao.count(sql1,params);
        Long commentCount = commentDao.count(sql2,params);

        request.setAttribute("postCount",postCount);
        request.setAttribute("commentCount",commentCount);


    }

}
